package net.dgg.rz.api.production.service;

import net.dgg.iboss.base.util.DateUtil;
import net.dgg.rz.api.production.dao.WorkBentchDao;
import net.dgg.rz.common.constant.ModuleCodeConstant;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.constant.ScWorkingConstant;
import net.dgg.rz.common.excption.CommonExcption;
import net.dgg.rz.common.utils.DataPermissionUtils;
import net.dgg.rz.common.utils.DateUtils;
import net.dgg.rz.common.utils.StringUtils;
import net.dgg.rz.common.utils.ValidateUtils;
import net.dgg.rz.production.entity.dto.ProductOrderDto;
import net.dgg.rz.production.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Service
public class WorkBentchService {

    @Autowired
    private WorkBentchDao workBentchDao;

    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    DataPermissionUtils dataPermissionUtils;

    public Integer getWorkBentchNum(Map paramMap) {
        synchronized (this) {
            Integer i = null;
            String code = null == paramMap.get("code") ? "" : paramMap.get("code").toString();
            if (code.equals(ScWorkingConstant.WORKING_RECEIVE_NUM)) {//接单量
                i = workBentchDao.countReceiveNum(paramMap);

            }else if (code.equals(ScWorkingConstant.WORKING_SUBMIT_NUM)) {//报审量
                i = workBentchDao.countSubmitNum(paramMap);

            } else if (code.equals(ScWorkingConstant.WORKING_SUBMIT_OK)) {//报审通过
                i = workBentchDao.countSubmitOK(paramMap);

            } else if (code.equals(ScWorkingConstant.WORKING_ORDER_COMPLETE)) {//成单量
                i = workBentchDao.countOrderCompleteNum(paramMap);

            } else if (code.equals(ScWorkingConstant.WORKING_LOAN_AMOUNT)) {//放款金额
                i = workBentchDao.countLoanAmountNum(paramMap) / 100;

            } else if (code.equals(ScWorkingConstant.WORKING_ORDER_ABANDON)) {//已废单
                paramMap.put("status", ScProductConstant.RZSCORDER_STATUS08);
                i = workBentchDao.countAbandonNum(paramMap);

            } else if (code.equals(ScWorkingConstant.WORKING_ORDER_RETURENED)) {//已退单
                paramMap.put("status", ScProductConstant.RZSCORDER_STATUS06);//已退单
                i = workBentchDao.countAbandonNum(paramMap);

            }if (code.equals(ScWorkingConstant.WORKING_WAITING_RECEIVE)) {//待接收
                Map params = new HashMap();
                params.put("target","0");
                List<ProductOrderDto> productOrderDtos = productOrderService.processingscOrderWithPage(params);
                //i = workBentchDao.countWaitingReceive(paramMap);
                i = productOrderDtos == null?0:productOrderDtos.size();

            } else if (code.equals(ScWorkingConstant.WORKING_ORDER_PROCESS)) {//办理中
                Map params = new HashMap();
                params.put("target","1");
                List<ProductOrderDto> productOrderDtos = productOrderService.processingscOrderWithPage(params);
                i = productOrderDtos == null?0:productOrderDtos.size();

            } else if (code.equals(ScWorkingConstant.WORKING_RETURENED_AUDIT)) {//退单审核中
                Map params = new HashMap();
                params.put("target","0");
                List<ProductOrderDto> productOrderDtos = productOrderService.auditOrderWithPage(params);
                i = productOrderDtos == null?0:productOrderDtos.size();

            } else if (code.equals(ScWorkingConstant.WORKING_ABANDON_AUDIT)) {//废单审核中
                Map params = new HashMap();
                params.put("target","1");
                List<ProductOrderDto> productOrderDtos = productOrderService.auditOrderWithPage(params);
                i = productOrderDtos == null?0:productOrderDtos.size();

            } /*else if (code.equals(ScWorkingConstant.WORKING_FOLLOW_TODAY)) {//今日待跟进
                paramMap.put("processStatus", ScStatusConstant.ORDER_PROCESS);// 今日待跟进
                i = backlogDao.countFollowToday(paramMap);

            } else if (code.equals(ScWorkingConstant.WORKING_NODE_DATEOUT)) {//节点超期
                paramMap.put("status", OverdueStateConstant.TIME_OUT);
                paramMap.put("processStatus", ScStatusConstant.ORDER_PROCESS);// 办理中
                i = backlogDao.countNodeDateout(paramMap);

            } else if (code.equals(ScWorkingConstant.WORKING_ORDER_DATEOUT)) {//订单超期
                paramMap.put("status", OverdueStateConstant.TIME_OUT);
                paramMap.put("processStatus", ScStatusConstant.ORDER_PROCESS);// 办理中
                i = backlogDao.countOrderDateout(paramMap);

            } else if (code.equals(ScWorkingConstant.WORKING_FOLLOW_DATEOUT)) {//超期跟进
                paramMap.put("processStatus", ScStatusConstant.ORDER_PROCESS);// 办理中
                i = backlogDao.countFollowDateout(paramMap);

            }*/
            return i;
        }
    }

    public String countRzData(Map paramMap){
        synchronized (this) {
            String resultNum  = "0";
            ValidateUtils.strNotEmpty(paramMap.get("code"),CommonExcption.class,"code参数不能为空！");
            //当countTimeType 么有传的时候 默认0当天
            if(StringUtils.isEmpty(paramMap.get("countTimeType"))){
                paramMap.put("countTimeType","-1");
            }else if(!"0".equals(paramMap.get("countTimeType")) && !"1".equals(paramMap.get("countTimeType")) && !"2".equals(paramMap.get("countTimeType"))){
                throw new CommonExcption("countTimeType参数值有误，countTimeType只能为0:当前，1:本月，2上个月");
            }
            //code 常量里面的状态
            String code = paramMap.get("code").toString();
            //countTimeType 0 今天 1 本月 2 上个月
            String countTimeType = paramMap.get("countTimeType").toString();
            Date now = new Date();
            if("0".equals(countTimeType)){
                String dateStr = DateUtils.getDayOfThisDay(now);
                paramMap.put("thisDay",dateStr);
            }else if("2".equals(countTimeType)){
                paramMap.put("lastMonth", DateUtils.getDateOfLastMonth(now));
            }else if("1".equals(countTimeType)){
                paramMap.put("thisMonth", DateUtils.getDateOfThisMonth(now));
            }
            if(ScWorkingConstant.WORKING_RECEIVE_NUM.equals(paramMap.get("code"))){//接单统计
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_PROCESSING_CODE);
                resultNum = workBentchDao.countReceiveNumByDateType(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_ORDER_COMPLETE.equals(paramMap.get("code"))){//已完结
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_COMPLETE_CODE);
                resultNum = workBentchDao.countCompleteNumByDateType(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_LOAN_NUM.equals(paramMap.get("code"))){//已放款总数
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_ALLORDER_CODE);
                resultNum = workBentchDao.countLoanNumByDateType(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_LOAN_AMOUNT.equals(paramMap.get("code"))){//已放款金额总数
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_ALLORDER_CODE);
                resultNum = StringUtils.isEmpty(workBentchDao.countLoanMoneyByDateType(paramMap)) ? "0.00" : workBentchDao.countLoanMoneyByDateType(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_ORDER_RETURENED.equals(paramMap.get("code"))){//已退单
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_ALLORDER_CODE);
                resultNum = workBentchDao.countReturendNumByDateType(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_ORDER_ABANDON.equals(paramMap.get("code"))){//已废单
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_ALLORDER_CODE);
                resultNum = workBentchDao.countBandonNumByDateType(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_WAITING_RECEIVE_TURN.equals(paramMap.get("code"))){//转单待接收
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_PROCESSING_CODE);
                resultNum = workBentchDao.countWaitingReceiveTurn(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_WAITING_RECEIVE_ORDER.equals(paramMap.get("code"))){//下单待接收
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_PROCESSING_CODE);
                resultNum = workBentchDao.countWaitingReceiveOrder(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_WAITING_RECEIVE_RETURN.equals(paramMap.get("code"))){//退回待接收
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_PROCESSING_CODE);
                resultNum = workBentchDao.countWaitingReceiveReturn(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_WAITING_ALLOCATON.equals(paramMap.get("code"))){//待分配
                dataPermissionUtils.productOrgDataAccess(paramMap,ModuleCodeConstant.RZSC_ALLOTORDER_CODE);
                resultNum = workBentchDao.countWaitingAllocation(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_ORDER_ALLOCATON.equals(paramMap.get("code"))){//已分配
                dataPermissionUtils.allotRecordDataAccess(paramMap);
                resultNum = workBentchDao.countOrderAllocation(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_ORDER_ALLOCATON_XY.equals(paramMap.get("code"))){//已分配(信用贷款)
                paramMap.put("typeCode",ScWorkingConstant.BUS_YT_DK_XYDK);
                dataPermissionUtils.allotRecordDataAccess(paramMap);
                resultNum = workBentchDao.countOrderAllocation(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_ORDER_ALLOCATON_DY.equals(paramMap.get("code"))){//已分配(抵押)
                paramMap.put("typeCode",ScWorkingConstant.BUS_YT_DK_DYDK);
                dataPermissionUtils.allotRecordDataAccess(paramMap);
                resultNum = workBentchDao.countOrderAllocation(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_ORDER_ALLOCATON_ZJ.equals(paramMap.get("code"))){//已分配(资金业务)
                paramMap.put("typeCode",ScWorkingConstant.BUS_YT_DK_ZJYW);
                dataPermissionUtils.allotRecordDataAccess(paramMap);
                resultNum = workBentchDao.countOrderAllocation(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_ORDER_ALLOCATON_OTHERS.equals(paramMap.get("code"))){//已分配(其他)
                paramMap.put("typeCode",ScWorkingConstant.BUS_YT_DK_QT);
                dataPermissionUtils.allotRecordDataAccess(paramMap);
                resultNum = workBentchDao.countOrderAllocation(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_ORDER_SUSPENT.equals(paramMap.get("code"))){//暂停
                paramMap.put("status",ScProductConstant.RZSCORDER_STATUS03);
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_PROCESSING_CODE);
                resultNum = workBentchDao.countOthers(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_RETURENED_AUDIT_SUPERIOR.equals(paramMap.get("code"))){//退单待审核
                paramMap.put("status",ScProductConstant.RZSCORDER_STATUS05);
                dataPermissionUtils.auditorDataAccess(paramMap);
                resultNum = workBentchDao.countOthers(paramMap)+"";
            }else if(ScWorkingConstant.WORKING_ABANDON_AUDIT_SUPERIOR.equals(paramMap.get("code"))){//废单待审核
                paramMap.put("status",ScProductConstant.RZSCORDER_STATUS07);
                dataPermissionUtils.auditorDataAccess(paramMap);
                resultNum = workBentchDao.countOthers(paramMap)+"";
            }
            return resultNum;
        }
    }

    public int beyondCount(Map paramMap){
        synchronized (this) {
            int resultNum  = 0;
            ValidateUtils.strNotEmpty(paramMap.get("code"),CommonExcption.class,"code参数不能为空！");
            //code 常量里面的状态
            String code = paramMap.get("code").toString();
            //分配超期统计
            if(ScWorkingConstant.DISTRIBUTION_ORDER_DATEOUT.equals(code)){
                paramMap.put("distribution",true);
                dataPermissionUtils.productOrgDataAccess(paramMap,ModuleCodeConstant.RZSC_ALLOTORDER_CODE);
                resultNum = workBentchDao.countBeyond(paramMap);
            }
            //接收超期统计
            if(ScWorkingConstant.RECEIVE_ORDER_DATEOUT.equals(code)){
                paramMap.put("receive",true);
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_PROCESSING_CODE);
                resultNum = workBentchDao.countBeyond(paramMap);
            }
            //订单超期统计
            if(ScWorkingConstant.WORKING_ORDER_DATEOUT.equals(code)){
                paramMap.put("order",true);
                dataPermissionUtils.flowUserDataAccess(paramMap, ModuleCodeConstant.RZSC_PROCESSING_CODE);
                resultNum = workBentchDao.countBeyond(paramMap);
            }
            return resultNum;
        }
    }


}
